package com.itclass.auth.pojo.dto;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.itclass.auth.pojo.db.SysRole;
import com.itclass.auth.pojo.db.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class AuthUserDetails implements UserDetails {
    private static final String ROLE = "ROLE_";

    private Long userId;

    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 姓名
     */
    private String name;
    /**
     * 手机
     */
    private String phone;

    /**
     * 头像地址
     */
    private String headUrl;
    /**
     * 部门id
     */
    private Long deptId;
    /**
     * 岗位id
     */
    private Long postId;
    /**
     * 描述
     */
    private String description;
    /**
     * openId
     */
    private String openId;

    /**
     * 状态（1：正常 0：停用）
     */
    private Integer status;
    /**
     * 账号是否启用
     */

    private boolean enabled;


    private List<String> roles;



    public static AuthUserDetails buildUserDetail(SysUser sysUser) {
        AuthUserDetails userDetailsDTO = new AuthUserDetails();
        userDetailsDTO.setUserId(sysUser.getId());
        userDetailsDTO.setName(sysUser.getName());
        userDetailsDTO.setUsername(sysUser.getUsername());
        userDetailsDTO.setPassword(sysUser.getPassword());
        userDetailsDTO.setStatus(sysUser.getStatus());
        userDetailsDTO.setEnabled(sysUser.getIsDeleted() == 1);


        userDetailsDTO.setHeadUrl(sysUser.getHeadUrl());
        userDetailsDTO.setDeptId(sysUser.getDeptId());
        userDetailsDTO.setPhone(sysUser.getPhone());
        userDetailsDTO.setDescription(sysUser.getDescription());

        List<SysRole> roleList = sysUser.getRoleList();

        if (!CollectionUtils.isEmpty(roleList)) {

            List<String> authorities = roleList.stream().map(SysRole::getRoleCode).collect(Collectors.toList());
            userDetailsDTO.setRoles(authorities);
        }


        return userDetailsDTO;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        if (!CollectionUtils.isEmpty(roles)) {
            for (String authority : roles) {
                grantedAuthorities.add(new SimpleGrantedAuthority(ROLE + authority));
            }
        }
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
